import router from './router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken, removeToken } from '@/utils/auth'

// 白名单 接数据的时候再说吧
const whiteList = [
  '/login', '/signUp',
  '/dashboard',
  '/links',
  '/courseNews', '/courseNews/courseNewsList', '/courseNews/newsDetails',
  '/announcement', '/announcement/announcementList', '/announcement/announcementDetails',
  '/subjectOverview', '/subjectOverview/synopsis', '/subjectOverview/situation', '/subjectOverview/goalProgramming', '/subjectOverview/synopsisList', '/subjectOverview/situationList', '/subjectOverview/goalProgrammingList',
  '/generalSituation', '/generalSituation/course', '/generalSituation/courseList', '/generalSituation/netLibrary', '/generalSituation/netLibraryDetail', '/generalSituation/introduction', '/generalSituation/introductionList',
  '/onlineClass', '/onlineClass/regulations', '/onlineClass/regulationsList',
  '/talentCultivation', '/talentCultivation/scheme', '/talentCultivation/reformation', '/talentCultivation/result', '/talentCultivation/teacher',
  '/academicResearch', '/academicResearch/subjectsDetails', '/academicResearch/subjects', '/academicResearch/resultDetails', '/academicResearch/result',
  '/teacherTeam', '/teacherTeam/foregoer', '/teacherTeam/foregoerDetails',
  '/discipline', '/discipline/regulatory', '/discipline/regulatoryDetail',
  '/forum', '/forum/article', '/forum/forumContent', '/forum/writeArticle',
  '/personalCenter', '/personalCenter/myArticle'
]
// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 会在任意路由跳转前执行，next一定要记着执行，不然路由不能跳转了
  store.dispatch('setParentRoute', from) // 将上级路由存至vuex
  NProgress.start()
  if (getToken()) {
    if (to.path === '/login' || to.path === '/register') {
      // if choose login , remove token, same logout
      removeToken()
      next()
    } else {
      if (store.getters.roles.length === 0) {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // 会在任意路由跳转后执行
  NProgress.done() // End Progress
})
